package com.quark.framework.configuration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import com.quark.framework.base.QuarkPojoBase;
import com.quark.framework.utility.QuarkLog;

public class QuarkConfigBase extends QuarkPojoBase {
	
	private File configFile = null;
	
	public QuarkConfigBase()
	{
	}
	
	public String loadFileContent()
	{
		String content = "";
		try {
			BufferedReader reader = new BufferedReader(new FileReader(configFile));
			String line = "";
			while((line = reader.readLine()) != null)
			{
				content += line;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			QuarkLog.log(e);
		} catch (IOException e){
			QuarkLog.log(e);
		}
		return content;
	}
	
	public boolean loadFromJSON(String file)
	{
		configFile = new File(file);
		if(!configFile.exists())
			return false;
		
		String fileContent = loadFileContent();
		JSON json = JSONSerializer.toJSON(fileContent);
		if(json.getClass().equals(JSONObject.class))
			//accept only jsonobject
		{
			JSONObject object = (JSONObject)json;
			if(object.isEmpty())
				return false;
			String[] names = this.getMemberNames();
			for(String i : names)
			{
				if(object.containsKey(i))
				{
					Class<?> type = this.getMember(i).getType();
					if(type.equals(String.class))
					{
						this.setValue(i, object.getString(i));
					}
					else if(type.equals(Integer.class))
					{
						this.setValue(i, object.getInt(i));
					}
					else if(type.equals(Long.class))
					{
						this.setValue(i, object.getLong(i));
					}
					else if(type.equals(Double.class))
					{
						this.setValue(i, object.getDouble(i));
					}
					else if(type.equals(Boolean.class))
					{
						this.setValue(i, object.getBoolean(i));
					}
					else if(type.isArray())
					{
						JSONArray array = object.getJSONArray(i);
						Class<?> realClass = type.getComponentType();
						Object[] ret = null; 
						if(realClass.equals(String.class))
						{
							ret = new String[array.size()];
						}
						else if(realClass.equals(Integer.class))
						{
							ret = new Integer[array.size()];
						}
						else if(realClass.equals(Long.class))
						{
							ret = new Long[array.size()];
						}
						else if(realClass.equals(Double.class))
						{
							ret = new Double[array.size()];
						}
						else if(realClass.equals(Float.class))
						{
							ret = new Float[array.size()];
						}
						else
						{
							QuarkLog.logError("Invalid type for array:" + type.getName());
							return false;
						}
						for(int j = 0; j < array.size(); j ++)
						{
							ret[j] = array.get(j);
						}
						this.setValue(i, ret);
					}
					else
					{
						QuarkLog.logError("Invalid type for config:" + type.getName());
					}
				}
			}
		}
		return true;
	}
	
	
}
